package com.snail.snailvr.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import com.snail.snailvr.R;
import com.snail.snailvr.utils.LockUtil;
import com.snail.snailvr.widget.CustomLockView;
import java.util.Timer;
import java.util.TimerTask;
import butterknife.Bind;

/**
 * 设置手势密码页面
 * Created by shiq on 6/20/16.
 */
public class GestureSetActivity extends GestureBaseActivity {

    private TextView tvWarn;

    private int times = 0;

    private int[] mIndexs = null;

    @Bind(R.id.cl)
    CustomLockView cl;

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void handleIntent() {}

    public static Intent getIntent(Activity activity){
        Intent intent = new Intent(activity, GestureSetActivity.class);
        return intent;
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initView() {
        setupToolbar(toolbar, getResources().getString(R.string.gesture_password), -1);

        tvWarn = getViewById(R.id.tvWarn);

        cl.setOnCompleteListener(new CustomLockView.OnCompleteListener() {
            @Override
            public void onComplete(int[] indexs) {
                mIndexs = indexs;

                // 显示次数
                if(times == 0){
                    tvWarn.setText(getResources().getString(R.string.draw_pattern_again));
                    tvWarn.setTextColor(getResources().getColor(R.color.wifi_text_color_normal));
                    times++;
                }
                else if(times == 1){
                    tvWarn.setText(getResources().getString(R.string.Success));
                    tvWarn.setTextColor(getResources().getColor(R.color.gesture_blue));
                    // 将密码设置在本地
                    LockUtil.setPwdToDisk(GestureSetActivity.this, mIndexs);
                    //LockUtil.setPwdStatus(GestureSetActivity.this, true);

                    invalidateSuccess();
                }
            }

            @Override
            public void onError(String msg) {
                tvWarn.setText(msg);
                tvWarn.setTextColor(getResources().getColor(R.color.gesture_red));

                TranslateAnimation animation = new TranslateAnimation(0, -10, 0, 0);
                animation.setInterpolator(new OvershootInterpolator());
                animation.setDuration(40);
                animation.setRepeatCount(4);
                animation.setRepeatMode(Animation.REVERSE);
                tvWarn.startAnimation(animation);
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_gesture_set;
    }

    @Override
    protected boolean onToolbarMenuItemClick(MenuItem item) {
        return false;
    }

    private void invalidateSuccess() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                finish();
            }
        };

        Timer timer = new Timer();

        timer.schedule(task, 1500);
    }
}